//
//  CommodityWarehouseViewController.m
//  BusinessApp
//
//  Created by CccDaxIN on 2016/10/27.
//  Copyright © 2016年 macox. All rights reserved.
//

#import "CommodityWarehouseViewController.h"
#import "CommodityWarehouseTableViewCell.h"
#import "ReleaseCommodityModel.h"
#import "CommoditySearchViewController.h"
@interface CommodityWarehouseViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UIScrollView *commodityWScrollView;
    NSString *buttonTagNumber;
    NSInteger ERTag;
    UITableViewCell *MyCell;
    NSString *string_ID;
}
@property (nonatomic,strong)UITableView *commodityWTableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)NSMutableArray *ERarray;
@property (nonatomic,strong)UIButton *ERButton;
@property (nonatomic,strong)UIView *viewCell;

@property (nonatomic,strong)NSString *wocao;
@end

@implementation CommodityWarehouseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"商品库";
    self.view.backgroundColor = [UIColor whiteColor];
    self.dataArray = [[NSMutableArray alloc]init];
    self.ERarray = [[NSMutableArray alloc]init];
    [self commodityClassificationDataRequest];

    [self createCommodityWarehouseTableView];
    [self createCommodityWarehouseScrollView];
    /*
     *返回
     */
    UIImage *commodityWarehouseBackImage = [UIImage imageNamed:@"左箭头"];
    commodityWarehouseBackImage = [commodityWarehouseBackImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:commodityWarehouseBackImage style:UIBarButtonItemStylePlain target:self action:@selector(commodityWarehouseBackImageBackAction)];
    
    /*
     *搜索
     */
    UIImage *searchImage = [UIImage imageNamed:@"白色放大镜"];
    searchImage = [searchImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:searchImage style:UIBarButtonItemStylePlain target:self action:@selector(searchImageAction)];
    
    _wocao = @"wocao";
    // Do any additional setup after loading the view.
}
- (void)commodityWarehouseBackImageBackAction{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -- 搜索
- (void)searchImageAction{
    NSLog(@"搜索");
    CommoditySearchViewController *commoditySearch = [[CommoditySearchViewController alloc]init];
    [self.navigationController pushViewController:commoditySearch animated:YES];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    [NNSingleton sharedSingleton].releasePosition = @"商品库";
    [NNSingleton sharedSingleton].allAvailable = @"";
}

#pragma mark --  创建tableview
- (void)createCommodityWarehouseTableView{

    _commodityWTableView =[[UITableView alloc]initWithFrame:CGRectMake(0,0,90,kScrernHeight+50) style:UITableViewStylePlain];
    _commodityWTableView.showsVerticalScrollIndicator = FALSE;
    _commodityWTableView.delegate=self;
    _commodityWTableView.dataSource=self;
    _commodityWTableView.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.94 alpha:1.00];
    _commodityWTableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_commodityWTableView];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(90, 0, 1, kScrernHeight)];
    view.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.93 alpha:1.00];
    [self.view addSubview:view];
    /**左侧一级绿色竖条
     */
    _viewCell = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 5,50)];
    _viewCell.backgroundColor = [UIColor colorWithHue:0.21 saturation:1.00 brightness:0.69 alpha:1.00];
    [_commodityWTableView addSubview:_viewCell];
  
}
#pragma mark -- 单独创建scrollview
- (void)createCommodityWarehouseScrollView{

    commodityWScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(91,64, kScrernWidth-91, 50)];
    commodityWScrollView.showsVerticalScrollIndicator = FALSE;
    commodityWScrollView.contentSize = CGSizeMake(kScrernWidth,0);
    commodityWScrollView.delegate = self;
    commodityWScrollView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:commodityWScrollView];
    
    ERTag = 10;
    for (int i = 0 ; i < _ERarray.count; i++) {
        ReleaseCommodityModel *modelSub= [_ERarray objectAtIndex:i];
        _ERButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_ERButton addTarget:self action:@selector(CommodityWarehouseERButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        _ERButton.tag = ERTag++;
        _ERButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [_ERButton setTitleColor:[UIColor colorWithHue:0.00 saturation:0.00 brightness:0.28 alpha:1.00] forState:UIControlStateNormal];
        [_ERButton.layer setMasksToBounds:YES];
        //边框宽度
        [_ERButton.layer setBorderWidth:1.0];
        _ERButton.layer.borderColor=[UIColor colorWithHue:0.00 saturation:0.00 brightness:0.93 alpha:1.00].CGColor;
        _ERButton.frame = CGRectMake(0+60*i,0,60,50);
        [_ERButton setTitle:modelSub.Name forState:UIControlStateNormal];
        [commodityWScrollView addSubview:_ERButton];
    }
    commodityWScrollView.contentSize = CGSizeMake(_ERarray.count*60,0);
}
#pragma mark -- 一级分类管理父类数据请求
- (void)commodityClassificationDataRequest{
    [GDJHttpTool get:TwolevelClassification params:@{@"ShouJiHao":GETPHONE,@"Duuid":GETUUID,@"Token":GETTOKEN} success:^(id responseObj) {
        NSLog(@"类似爱情 %@",responseObj[@"Category"]);
        [SVProgressHUD showWithStatus:@"正在加载"];

        //查看错误码
        NSInteger err = [[responseObj valueForKey:@"Err"]integerValue];
        NSLog(@"莫文蔚%ld",(long) err);
        if (err == 0) {
            [SVProgressHUD dismiss];
            self.dataArray =   [ReleaseCommodityModel mj_objectArrayWithKeyValuesArray:[responseObj objectForKey:@"Category"]];
        }else{
            NSLog(@"%@",responseObj[@"ErrMsg"]);
            [SVProgressHUD showErrorWithStatus:responseObj[@"ErrMsg"]];
            return;
        }
        [_commodityWTableView reloadData];
        ReleaseCommodityModel *modelSub= [_dataArray objectAtIndex:0];
        [NNSingleton sharedSingleton].subId = modelSub.Id;
        [self commodityClassificationDataSubRequest];//子类
    }failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_dataArray count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifer=@"Mycell";
    MyCell=[tableView cellForRowAtIndexPath:indexPath];
    if (MyCell==nil) {
        MyCell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        MyCell.textLabel.font = [UIFont systemFontOfSize:14];
    }
    MyCell.selectedBackgroundView = [[UIView alloc] initWithFrame:self.view.frame];
    MyCell.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
    /**判断位置改变字体颜色
     */
    if ([_wocao isEqualToString:@"wocao"]) {
        if (indexPath.row == 0) {
           MyCell.textLabel.textColor = [UIColor colorWithHue:0.22 saturation:0.54 brightness:0.75 alpha:1.00];
        }else{
             MyCell.textLabel.textColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.28 alpha:1.00];
        }
    }else{
        if (indexPath.row == [_wocao integerValue]) {
             MyCell.textLabel.textColor = [UIColor colorWithHue:0.22 saturation:0.54 brightness:0.75 alpha:1.00];
        }else{
              MyCell.textLabel.textColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.28 alpha:1.00];
        }
    }
    ReleaseCommodityModel *model= [_dataArray objectAtIndex:indexPath.row];
    MyCell.textLabel.text = model.Name;
    return MyCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [commodityWScrollView removeFromSuperview];
    /**改变字体用获取位置
     */
    _wocao = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    [_commodityWTableView reloadData];
/*****/
    [_commodityWarView removeFromSuperview];//remove试图
    _viewCell.frame = CGRectMake(0, indexPath.row*50, 5, 50);
    _commodityWarView = nil;
    buttonTagNumber = @"0";

    ReleaseCommodityModel *modelSub= [_dataArray objectAtIndex:indexPath.row];
    [NNSingleton sharedSingleton].subId = modelSub.Id;
    [self commodityClassificationDataSubRequest];//子类
}
#pragma mark -- 二级分类管理子类数据请求
- (void)commodityClassificationDataSubRequest{
    [_ERarray removeAllObjects];
    buttonTagNumber = @"0";
    [commodityWScrollView removeFromSuperview];
    MyCell.textLabel.textColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.28 alpha:1.00];

    NSString *subID_str = [NSString stringWithFormat:@"%d",[NNSingleton sharedSingleton].subId];
    [GDJHttpTool get:TwolevelClassificationSub params:@{@"ShouJiHao":GETPHONE,@"Duuid":GETUUID,@"Token":GETTOKEN,@"ParentId":subID_str} success:^(id responseObj) {
        NSLog(@"海若有因 %@",responseObj);
        
        //查看错误码
        NSInteger err = [[responseObj valueForKey:@"Err"]integerValue];
        NSLog(@"争取吧%ld",(long) err);
        if (err == 0) {
            if ([responseObj objectForKey:@"SecondCategory"] == [NSNull alloc]) {
                buttonTagNumber = @"3";
            }else{
            _ERarray =   [ReleaseCommodityModel mj_objectArrayWithKeyValuesArray:[responseObj objectForKey:@"SecondCategory"]];
                [self createCommodityWarehouseScrollView];
           }
            [self commodityLibraryInformationDataRequest];
        }else{
            NSLog(@"%@",responseObj[@"ErrMsg"]);
            [SVProgressHUD showErrorWithStatus:responseObj[@"ErrMsg"]];
            return;
        }
    }failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}
- (void)CommodityWarehouseERButtonAction:(UIButton *)sender{
    [self commodityLibraryInformationDataRequest];
    buttonTagNumber = @"1";
    NSInteger tagNumber = sender.tag-10;

    ReleaseCommodityModel *modelSub= [_ERarray objectAtIndex:tagNumber];
    string_ID = [NSString stringWithFormat:@"%ld",(long)modelSub.Id];
    NSLog(@"一万个理由  %@",string_ID);
    /**显示button边框
     **/
    _ERButton.layer.borderColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.92 alpha:1.00].CGColor;
    
    _ERButton = (UIButton *)[self.view viewWithTag:sender.tag];
    _ERButton.layer.borderColor = [UIColor colorWithHue:0.22 saturation:0.54 brightness:0.75 alpha:1.00].CGColor;

    [self commodityLibraryInformationDataRequest];
}
#pragma mark -- 获取商品库信息
- (void)commodityLibraryInformationDataRequest{
    NSString *stringID;
    NSString *hidden;
    if (([buttonTagNumber isEqualToString:@"0"])) {//为选中二级分类第0个全部
        if (_ERarray.count != 0) {
            hidden = @"";
            ReleaseCommodityModel *modelSub= [_ERarray objectAtIndex:0];
            stringID = [NSString stringWithFormat:@"%d",modelSub.Id];
            NSLog(@"-------真心英雄 %@",stringID);
            _ERButton = (UIButton *)[self.view viewWithTag:10];
            _ERButton.layer.borderColor = [UIColor colorWithHue:0.22 saturation:0.54 brightness:0.75 alpha:1.00].CGColor;
        }else{//如果二级分类为nil就取一级分类id
            hidden = @"yes";
            stringID = [NSString stringWithFormat:@"%d",[NNSingleton sharedSingleton].subId];
        }
    }else {
        /**选中的二级分类id
         */
        hidden = @"";
        NSLog(@"真心英雄 %@",string_ID);
        stringID = string_ID;
    }
    [NNSingleton sharedSingleton].ERJIID = stringID;
    /*二级分类下的商品库
     **/
    if ([hidden isEqualToString:@"yes"]) {
        [NNSingleton sharedSingleton].allAvailable = @"无";
        _commodityWarView = [[CommodityWarehouseView alloc]init];
        
        _commodityWarView.frame = CGRectMake(91,64, kScrernWidth-90, kScrernHeight);
    }else{
        [NNSingleton sharedSingleton].allAvailable = @"有";
        _commodityWarView = [[CommodityWarehouseView alloc]init];
        _commodityWarView.frame = CGRectMake(91,114, kScrernWidth-90, kScrernHeight);
    }
    [self.view addSubview:_commodityWarView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
